SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE dbo.SP_GEN_ID (@DESC_GENERATOR NVARCHAR(50), @VL_GENERATOR INT, @RETORNO INTEGER OUTPUT)
AS
BEGIN
	DECLARE @TEMP NVARCHAR(1000), @TEMP_SQL NVARCHAR(1000), @TEMP_RESULT NUMERIC(18,4), @CONT INT
	
	SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
	
	BEGIN TRAN
		-- OBTEM NOME DA TABELA DE INCREMENTO
		SET @TEMP = 'dbo.AA_GEN_' + @DESC_GENERATOR
		SET @TEMP_SQL = ' INSERT INTO ' + @TEMP + ' VALUES( ' + CAST(@@SPID AS VARCHAR(10))  + ' ) '
		exec sp_executesql @TEMP_SQL, N'@TEMP_RESULT NUMERIC(18,4) out', @TEMP_RESULT out
		SET @RETORNO = @@IDENTITY
	COMMIT TRAN
	
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

